package convertx

import (
	"strconv"
)

func IntToString(ival interface{}) string {
	val := ""
	switch x := ival.(type) {
	case int:
		val = strconv.Itoa(x)
	case int8:
		val = strconv.Itoa(int(x))
	case int16:
		val = strconv.Itoa(int(x))
	case int32:
		val = strconv.Itoa(int(x))
	case int64:
		val = strconv.Itoa(int(x))
	}
	return val
}

func AnyToString(ival interface{}) string {
	val := ""
	switch x := ival.(type) {
	case string:
		val = x
	case int:
		val = strconv.Itoa(x)
	case int8:
		val = strconv.Itoa(int(x))
	case int16:
		val = strconv.Itoa(int(x))
	case int32:
		val = strconv.Itoa(int(x))
	case int64:
		val = strconv.Itoa(int(x))
	case uint:
		val = strconv.Itoa(int(x))
	case uint8:
		val = strconv.Itoa(int(x))
	case uint16:
		val = strconv.Itoa(int(x))
	case uint32:
		val = strconv.Itoa(int(x))
	case uint64:
		val = strconv.Itoa(int(x))
	case float32:
		val = strconv.Itoa(int(x))
	case float64:
		val = strconv.Itoa(int(x))
	case bool:
		if x {
			val = "1"
		} else {
			val = "0"
		}
	}
	return val
}

func AnyToBool(ival interface{}) bool {
	switch data := ival.(type) {
	case bool:
		return ival.(bool)
	case int:
	case int8:
	case int16:
	case int32:
	case int64:
	case uint:
	case uint8:
	case uint16:
	case uint32:
	case uint64:
	case float32:
	case float64:
		return data > 0
	case string:
		return data == "true" || data == "1"
	default:
	}

	return false
}

func AnyToUint(ival interface{}) uint {
	return uint(AnyToInt(ival))
}

func AnyToInt(ival interface{}) int {
	val := int(0)
	switch data := ival.(type) {
	case int:
		val = data
	case int8:
		val = int(data)
	case int16:
		val = int(data)
	case int32:
		val = int(data)
	case int64:
		val = int(data)
	case uint:
		val = int(data)
	case uint8:
		val = int(data)
	case uint16:
		val = int(data)
	case uint32:
		val = int(data)
	case uint64:
		val = int(data)
	case float32:
		val = int(data)
	case float64:
		val = int(data)
	case string:
		i, err := strconv.Atoi(data)
		if err != nil {
			val = 0
		} else {
			val = i
		}
	case bool:
		if data {
			val = 1
		}
	default:
	}

	return val
}

func AnyToInt64(ival interface{}) int64 {
	val := int64(0)
	switch data := ival.(type) {
	case int:
		val = int64(data)
	case int8:
		val = int64(data)
	case int16:
		val = int64(data)
	case int32:
		val = int64(data)
	case int64:
		val = data
	case uint:
		val = int64(data)
	case uint8:
		val = int64(data)
	case uint16:
		val = int64(data)
	case uint32:
		val = int64(data)
	case uint64:
		val = int64(data)
	case float32:
		val = int64(data)
	case float64:
		val = int64(data)
	case string:
		i, err := strconv.ParseFloat(data, 64)
		if err != nil {
			val = 0
		} else {
			val = int64(i + 0.5) // 四舍五入
		}
	case bool:
		if data {
			val = int64(1)
		}
	default:
	}

	return val
}

func AnyToFloat64(ival interface{}) float64 {
	val := float64(0)
	switch data := ival.(type) {
	case int:
		val = float64(data)
	case int8:
		val = float64(data)
	case int16:
		val = float64(data)
	case int32:
		val = float64(data)
	case int64:
		val = float64(data)
	case uint:
		val = float64(data)
	case uint8:
		val = float64(data)
	case uint16:
		val = float64(data)
	case uint32:
		val = float64(data)
	case uint64:
		val = float64(data)
	case float32:
		val = float64(data)
	case float64:
		val = data
	case string:
		i, err := strconv.ParseFloat(data, 64)
		if err != nil {
			val = 0
		} else {
			val = i
		}
	}

	return val
}
